package com.yuerlive.rainbow.module.live.presenter;

import com.yuerlive.rainbow.api.LiveApi;
import com.yuerlive.rainbow.api.LiveService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.LiveDetailData;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoyi on 2017/6/2.
 */

public class LiveRoomPresenter extends BasePresenterImpl implements LiveRoomContract.Presenter {
    private LiveRoomContract.View view;
    private LiveService liveService;
    private String TAG = "LiveRoomPresenter";

    public LiveRoomPresenter(LiveRoomContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        liveService = LiveApi.getInstance().liveService;
    }

    @Override
    public void getLiveDetail(String liveId) {
        view.showLoading();
        Map<String, String> params = new HashMap<>();
        User user = UserProxy.getUser();
        params.put("liveId", liveId);
        liveService.getLiveDetail(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<LiveDetailData>>(this) {
                    @Override
                    protected void onSuccess(BaseData<LiveDetailData> liveDetailDataBaseData) {
                        view.hideLoading();
                        view.showLiveDetail(liveDetailDataBaseData.getObject());
                        L.e("TAG", "detail   ====  " + liveDetailDataBaseData);
                    }

                    @Override
                    protected void onFail(String message) {
                        L.e("TAG", "onFail detail   ====  " + message);
                    }
                });
    }

    @Override
    public void leaveLiveRoom(String liveId) {
        Map<String, String> params = new HashMap<>();
        params.put("liveId", liveId);
        liveService.leaveLiveRoom(params)
                .compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        L.e(TAG, baseData);
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }
}
